package com.ftw.commons.utils

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.text.InputType
import android.text.TextUtils
import android.text.method.DigitsKeyListener
import android.widget.EditText
import com.ftw.commons.R
import com.ftw.commons.log.LogUtils
import com.ftw.commons.widgets.toast.ToastUtils
import java.util.regex.Pattern

/**
 * 描    述：
 * 作    者：fantianwen
 * 时    间：2019/2/12  10:15 AM
 */
object Utils {


    /**
     * 加密手机号
     */
    fun encryptionPhone(phoneNum: String): String {
        return if (TextUtils.isEmpty(phoneNum) && phoneNum.length < 11) {
            phoneNum
        } else phoneNum.substring(0, 3) + "****" +
                phoneNum.substring(phoneNum.length - 4, phoneNum.length)
    }


    fun copyContent(context: Context, strId: Int, content: String, label: String): Boolean {
        val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        if (null != clipboard) {
            val clip = ClipData.newPlainText(label, content)
            clipboard.primaryClip = clip
            ToastUtils.showNormal(context, context.getString(strId))
            return true
        }

        LogUtils.w("AppUtils copyContent ClipboardManager is null!")

        return false
    }


    /**
     * 从邮件地址拿用户姓名
     */
    fun getNameFromEmailAddress(emailAddress: String): String {
        return emailAddress.substring(
            0,
            emailAddress.indexOf("@")
        ).replace("<", "")
    }

    private const val MAIL_REG =
        "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"

    /**
     * 校验邮箱
     * @param email
     * @return 校验通过返回true，否则返回false
     */
    fun isEmail(email: String): Boolean {
        return Pattern.matches(MAIL_REG, email)
    }

    /**
     * 设置edittext只能输入数字英文字母
     */
    fun setEditTextInputNumberOrEnglishChar(context: Context, editText: EditText) {

        editText.keyListener = object : DigitsKeyListener() {

            override fun getInputType(): Int {
                return InputType.TYPE_TEXT_VARIATION_PASSWORD
            }

            override fun getAcceptedChars(): CharArray {
                return context.getString(R.string.wordAndNum).toCharArray()
            }
        }

    }

    /**
     * 校验私钥是否合法
     */
    fun isValidWifPrivateKey(key: String): Boolean {
        val regString = "[a-z0-9A-Z]{52}"
        return Pattern.matches(regString, key)
    }



}






















